// 确认交易 {"道具":[1],"数量":[1],"bb":{},"银子":"1000"}
// @ts-ignore
export async function execute(client_id,message_json) {
    let login_characters = G.Characters.get_current_login_characters(client_id);
    let trade_data = _CharactersTradeData.get(login_characters["id"]);
    if (trade_data === undefined) {
        ClientUtils.send_basic_prompt(client_id, "#R/当前未发起交易");
        return;
    }
    let target_characters = _CharactersData.get(trade_data["target_id"]);
    if (target_characters === undefined) {
        ClientUtils.send_basic_prompt(client_id, "#R/对方不在线");
        return;
    }
    if (trade_data["锁定"] && trade_data["确认"] === undefined) {
        trade_data["确认"] = true;
        let target_trade_data = _CharactersTradeData.get(trade_data["target_id"]);
        if (trade_data["锁定"] && trade_data["确认"] && target_trade_data["锁定"] && target_trade_data["确认"]) {
            finish_trade(trade_data, target_trade_data);
            return;
        }
        ClientUtils.send_basic_prompt(client_id, "#G/已确认交易,请等待对方确认");
        ClientUtils.send_characters_basic_prompt(target_characters["id"],  "#R/对方已确认交易,请尽快确认交易");
        return;
    }
    if (trade_data["锁定"] && trade_data["确认"]) {
        ClientUtils.send_basic_prompt(client_id, "#R/已确认交易,请勿重复操作");
        return;
    }
    trade_data["道具"] = [];
    trade_data["bb"] = [];
    trade_data["数量"] = [];
    trade_data["银子"] = 0;
    if (message_json["道具"].length !== undefined) {
        for (let index = 0; index < message_json["道具"].length; index++) {
            const item_index = message_json["道具"][index] - 1;
            if (login_characters["行囊"][item_index] === undefined) {
                ClientUtils.send_basic_prompt(client_id, "#R/要交易的物品不存在");
                return;
            }
            if (login_characters["行囊"][item_index]["数量"] < message_json["数量"][index]) {
                ClientUtils.send_basic_prompt(client_id, "#R/要交易的物品剩余数量不足");
                return;
            }
            let tmp_item = Object.assign({}, login_characters["行囊"][item_index]);
            tmp_item["index"] = item_index;
            trade_data["道具"].push(tmp_item);

        }
        trade_data["数量"] = message_json["数量"];
    }
    if (message_json["bb"].length) {
        for (let index = 0; index < message_json["bb"].length; index++) {
            const pet_index = message_json["道具"][index] - 1;
            let tmp_pet = Object.assign({}, login_characters["召唤兽"][pet_index]);
            tmp_pet["index"] = pet_index;
            trade_data["bb"].push(tmp_pet);

        }
    }
    if (login_characters["银子"] < Number(message_json["银子"])) {
        ClientUtils.send_basic_prompt(client_id, "#R/银子余额不足");
        return;
    }
    trade_data["银子"] = Number(message_json["银子"]);
    trade_data["锁定"] = true;
    ClientUtils.send_client_message(client_id, 3508, 1);
    ClientUtils.send_basic_prompt(client_id, "#G/你已锁定交易状态");
    ClientUtils.send_characters_basic_prompt(target_characters["id"],  "#R/对方已锁定交易状态");
    ClientUtils.send_characters_client_message(target_characters["id"], 3510, {
        "道具": trade_data["道具"],
        "bb": trade_data["bb"],
        "银子": trade_data["银子"],
        "数量": message_json["数量"]
    });
}

function finish_trade(trade_data, target_trade_data) {
    ClientUtils.send_characters_basic_prompt(trade_data["target_id"], "#G/交易完成");
    ClientUtils.send_characters_basic_prompt(target_trade_data["target_id"],"#G/交易完成");
    ClientUtils.send_characters_client_message(trade_data["target_id"], 3511, 1);
    ClientUtils.send_characters_client_message(target_trade_data["target_id"], 3511, 1);
    _CharactersTradeData.delete(trade_data["target_id"]);
    _CharactersTradeData.delete(target_trade_data["target_id"]);
}